/*
* This file is a part of the open-eBackup project.
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file, You can obtain one at
* http://mozilla.org/MPL/2.0/.
*
* Copyright (c) [2024] Huawei Technologies Co.,Ltd.
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*/
#include "FusionStorageClient.h"
#include "protect_engines/hcs/common/HcsHttpStatus.h"

namespace {
const std::string MODULE_NAME = "FusionStorageClient";
}

HCS_PLUGIN_NAMESPACE_BEGIN

bool FusionStorageClient::CheckParams(ModelBase &model)
{
    return true;
}

std::string FusionStorageClient::BuildQueryBody(GetFusionStorageRequest &request)
{
    Json::Value jsonReq;
    jsonReq["user_name"] = request.GetFusionStorageUserName();
    jsonReq["password"] = request.GetFusionStoragePassword();
    jsonReq["scope"] = "0";
    Json::FastWriter fastWriter;
    std::string body = fastWriter.write(jsonReq);
    return body;
}

std::shared_ptr<ResponseModel> FusionStorageClient::GetToken(GetFusionStorageRequest &request, AuthObj &storageAuth)
{
    RequestInfo requestInfo;
    requestInfo.m_method = "POST";
    requestInfo.m_resourcePath = "https://{ip}:{port}/api/v2/aa/sessions";
    requestInfo.m_pathParams["ip"] = request.GetEnvAddress();
    requestInfo.m_pathParams["port"] = request.GetFusionStoragePort();
    requestInfo.m_queryParams = {};
    requestInfo.m_headerParams["Content-Type"] = "application/json";
    requestInfo.m_body = BuildQueryBody(request);
    requestInfo.m_auth = storageAuth;
    std::shared_ptr<ResponseModel> response = std::make_shared<ResponseModel>();
    if (CallApi(requestInfo, response, request) != SUCCESS) {
        ERRLOG("Failed to send request. %s", (response -> GetErrString()).c_str());
        return response;
    }
    return response;
}

HCS_PLUGIN_NAMESPACE_END